<?
class cmsdream_code {
	public $imgWidth;
	public $imgHeight;
	public $imgFont;
	public $sessingName;

	public function __construct($n = 'cmsdream-v-code', $w = 82, $h = 30, $f = 6) {
		$this->imgWidth = $w;
		$this->imgHeight = $h;
		$this->imgFont = $f;
		$this->sessingName = $n;
		$cc = $this->getRandomCode();
		$_SESSION[$this->sessingName] = $cc;
		$this->doOutputImg($cc, $this->imgWidth, $this->imgHeight, $this->imgFont);
	}

	//获取随机数字字符串
	private function getRandomCode($length = 6) {
		$glbVerifySeed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$bgnIdx = 0;
		$endIdx = strlen($glbVerifySeed)-1;
		
		$code = "";
		for($i=0; $i<$length; $i++) {
			$curPos = rand($bgnIdx, $endIdx);
			$code .= substr($glbVerifySeed, $curPos, 1);
		}

		return $code;
	}
	
	//输出校验码图像
	private function doOutputImg($string, $imgWidth, $imgHeight, $imgFont,
		$imgFgColorArr=array(0,0,0), $imgBgColorArr=array(255,255,255)) {
		$image = imagecreatetruecolor($imgWidth, $imgHeight);

		//用白色背景加黑色边框画个方框
		$backColor = imagecolorallocate($image, 255, 255, 255);
		$borderColor = imagecolorallocate($image, 0, 0, 0);
		imagefilledrectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $backColor);
		imagerectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $borderColor);

		$imgFgColor = imagecolorallocate ($image, $imgFgColorArr[0], $imgFgColorArr[1], $imgFgColorArr[2]);
		$this->doDrawStr($image, $string, $imgFgColor, $imgFont);
		$this->doPollute($image, 64);

		header('Content-type: image/png');
		imagepng($image);
		imagedestroy($image);
	}

	//画出校验码
	private function doDrawStr($image, $string, $color, $imgFont) {
		$imgWidth = imagesx($image);
		$imgHeight = imagesy($image);

		$count = strlen($string);
		$xpace = ($imgWidth/$count);

		$x = ($xpace-6)/2;
		$y = ($imgHeight/2-8);
		for ($p = 0; $p<$count;  $p ++) {
			$xoff = rand(-2, +2);
			$yoff = rand(-2, +2);
			$curChar = substr($string, $p, 1);
			imagestring($image, $imgFont, $x+$xoff, $y+$yoff, $curChar, $color);
			$x += $xpace;
		}

		return 0;
	}
	
	//画出一些杂点
	private function doPollute($image, $times) {   
		$imgWidth = imagesx($image);
		$imgHeight = imagesy($image);
		for($j=0; $j<$times; $j++) {
			$x = rand(0, $imgWidth);
			$y = rand(0, $imgHeight);

			$color = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));
			imagesetpixel($image, $x, $y, $color);
		}
	}
}
